<template>
  <div>
    <h1>公园从业人员设备管理</h1>
    <el-divider />
  </div>
  <div>
    <el-form :model="search" ref="form" :inline="false" size="default" class="flex">
      <el-form-item label="">
        <i class="mr-5">关键字</i>
        <el-input v-model="search.name" placeholder="请输入关键字" class="ipt mr-10" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearch" size="large">搜索</el-button>
        <el-button @click="handleDel" size="large">重置</el-button>
      </el-form-item>
    </el-form>

    <el-button type="primary" size="default" @click="handleDrawer(null)">新增</el-button>
  </div>
  <el-table :data="parkData" border stripe style="width: 100%" height="260" :header-cell-style="headerCellStyle">
    <el-table-column label="序号" type="index" width="80px" />
    <el-table-column label="设备名称" prop="name" />
    <el-table-column label="设备编号" prop="number" />
    <el-table-column label="备注" prop="intro" />
    <el-table-column label="关联人员" prop="relevance" />
    <el-table-column label="操作" fixed="right">
      <template #default="scope">
        <div>
          <el-button type="primary" size="small" @click="handleDrawer(scope.row)">编辑</el-button>
          <el-popconfirm title="是否确定删除？" confirmButtonText="确认" cancelButtonText="取消" icon="WarningFilled"
            iconColor="#f90" @confirm="handleDele(scope.row, scope.$index)">
            <template #reference>
              <el-button type="danger" size="small">删除</el-button>
            </template>
          </el-popconfirm>
        </div>
      </template>
    </el-table-column>
  </el-table>
  <div class="flex justify-end mt-10">
    <el-pagination v-model:current-page="current" v-model:page-size="pageSize" :page-sizes="[15, 50, 100]" :small="false"
      :disabled="false" :background="true" layout="total, sizes, prev, pager, next, jumper" :total="parks.totals"
      @size-change="handleSizeChange" @current-change="handleCurrentChange" />
  </div>

  <el-drawer :title="rowData ? '编辑设备信息' : '新增设备信息'" v-model="drawer" direction="rtl" size="50%" :destroy-on-close="true"
    :show-close="true">
    <JobAdd :edata="rowData" @update:table-data="handleUpdate" />
  </el-drawer>
</template>
<script setup lang="ts">
import { reactive, ref, onMounted } from 'vue'
import { Overview, parkDel } from '../../api/Park/job'
import { usePark } from '../../store/modules/park'
import JobAdd from './Components/jobAdd.vue'
const parks = usePark()

const search = reactive({
  name: ''
})

let parkDatas = ref([])
onMounted(() => {
  Overview().then((res) => {
    console.log('公园信息', res.data.results)
    parkDatas.value = res.data.results
  })
})

const drawer = ref<boolean>(false)
// 搜索
const handleSearch = async () => {
  console.log(search)
  current.value = 1 //重新回到第一页
  let res = await Overview(1, pageSize.value, search)
  let { results, count } = res.data
  parkData.value = results
  total.value = count
  parks.totals = total.value
}

// 重置
const handleDel = () => {
  search.name = ''
  parkData.value = res.data.results
  total.value = count
  parks.totals = total.value
}
interface parkDataType {
  name: string
  number: string
  intro: string
  relevance: string
  objectId?: string
}
const parkData = ref<Array<parkDataType | null>>([])
const total = ref<number>(0)
let res = await Overview()
// console.log(res);

let { results, count } = res.data
parkData.value = results
total.value = count
parks.totals = total.value

// 编辑
const rowData = ref<parkDataType | null>(null) //存放新增、编辑、查看的数据包
const handleDrawer = (row: parkDataType | null) => {
  console.log('区分新增、编辑', row)
  drawer.value = true
  rowData.value = row
}

const handleUpdate = (newpark: parkDataType) => {
  drawer.value = false
  if (rowData.value) {
    // 编辑后,更新本地列表
    for (let i = 0; i < parkData.value.length; i++) {
      if (parkData.value[i]!.objectId == newpark.objectId) {
        parkData.value[i] = newpark
        return
      }
    }
  } else {
    parks.totals++
    // 新增后,更新本地列表
    parkData.value.push(newpark)
  }
}
// 删除
const handleDele = (row: parkDataType, idx: number) => {
  console.log('删除', row, idx)
  parkDel(row.objectId!).then(async () => {
    parkData.value.splice(idx, 1) //删除本地
    // parkData.value = res.data.results
    let res = await Overview(current.value, pageSize.value, search)
    let { results, count } = res.data
    parkData.value = results
    total.value = count
    parks.totals = total.value
    console.log(res)
  })
}

// 分页
const current = ref<number>(1)
const pageSize = ref<number>(10) //每页显示的数据条数
// 页码变化触发事件
const handleCurrentChange = async (page: number) => {
  console.log(page)
  let res = await Overview(page, pageSize.value, search)
  parkData.value = res.data.results
}
// 每页数量变化事件
const handleSizeChange = async () => {
  current.value = 1 //重新回到第一页
  let res = await Overview(1, pageSize.value, search)
  parkData.value = res.data.results
}

const headerCellStyle = ({ row, column, rowIndex, columnIndex }) => {
  if (columnIndex || columnIndex === 0) {
    return {
      backgroundColor: '#f3f4f6'
    }
    console.log(row, column, rowIndex)
  }
}
</script>

<style scoped>
.ipt {
  width: 300px;
  height: 50px;
}
</style>
